# albumviewer.py
import wx
from pysage import Actor, ActorManager, get_logger
import Image
import util
import wx.lib.evtmgr as em
from messages import *

logger = get_logger()

omanager = ActorManager.get_singleton()

DELETE_ALBUM_ID = wx.NewId()

class AlbumViewer(wx.VListBox, Actor):
    '''the list control that shows all album's highlights and gives a description about them'''
    subscriptions = ['InitializeAlbumsViewerMessage', 'AlbumPopUpMenuMessage']
    def __init__(self, *args, **kws):
        Actor.__init__(self)
        wx.VListBox.__init__(self, *args, **kws)
        self.Bind(wx.EVT_RIGHT_UP, omanager.find('albumpresenter').right_mouse_up)
    def handle_AlbumPopUpMenuMessage(self, msg):
        menu = wx.Menu()
        menu.Append(DELETE_ALBUM_ID, 'Remove')
        self.Bind(wx.EVT_MENU, lambda evt: omanager.queue_message(RemoveAlbumMessage(albumid = msg.get_property('albumid'))), id=DELETE_ALBUM_ID)
        self.PopupMenu(menu)
        menu.Destroy()
        return True
    def handle_InitializeAlbumsViewerMessage(self, msg):
        self.SetItemCount(len(omanager.find('mapi').get_albums()))
        self.Refresh()
    def edit_permissions(self, albumid):
        all = omanager.find('mapi').get_viewers()
        eligible = omanager.find('mapi').get_album_viewers(albumid)
        dlg = wx.MultiChoiceDialog(omanager.find('window'),
                                   "Select eligible viewers",
                                   "Permissions", [x.name for x in all])
        dlg.SetSelections([i for i, viewer in enumerate(all) if viewer in eligible])

        if (dlg.ShowModal() == wx.ID_OK):
            selections = dlg.GetSelections()
            omanager.find('albumcontroller').edit_album_users(albumid, [all[x].id for x in selections])
        dlg.Destroy()
    # This method must be overridden.  When called it should draw the
    # n'th item on the dc within the rect.  How it is drawn, and what
    # is drawn is entirely up to you.
    def OnDrawItem(self, dc, rect, n):
        if self.GetSelection() == n:
            c = wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)
        else:
            c = self.GetForegroundColour()
        dc.SetFont(self.GetFont())
        dc.SetTextForeground(c)
        
        # getting the album
        album = omanager.find('mapi').get_albums()[n]
        
        # get this folder's first picture
        if album.images:
            highlight = album.images[0]
            img = Image.open(highlight.path)
            size = util.get_best_size(img, (util.ALBUM_THUMB_SIZE.x, util.ALBUM_THUMB_SIZE.y))
            thumb = util.fancy_thumbnail(img, size, highlight.id, use_bitmap=True)
            dc.DrawBitmap(thumb, *rect.GetTopLeft())
        # dc.DrawLabel('album %s' % n, rect, wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
            
#        dc.DrawLabel(self._getItemText(n), rect,
#                     wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)

    # This method must be overridden.  It should return the height
    # required to draw the n'th item.
    def OnMeasureItem(self, n):
        return 50
        height = 0
        for line in self._getItemText(n).split('\n'):
            w, h = self.GetTextExtent(line)
            height += h
        return height + 5


    # These are also overridable:
    #
    def OnDrawSeparator(self, dc, rect, n):
        '''Draw a separator between items.  Note that rect may be reduced in size if desired so OnDrawItem gets a smaller rect.'''
        pass
    #
    # OnDrawBackground(dc, rect, n)
    #   Draw the background and maybe a border if desired.


        
        
        
        